/*
 * @Author: your name
 * @Date: 2021-05-30 08:12:29
 * @LastEditTime: 2022-04-17 10:53:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \telescope720P\src\gui_awtk.cpp
 */
#include "gui_awtk.h"
#include "xlog.h"
#include <thread>
extern bool initDone;
static ret_t OnKeyDownEvent(void* ctx, event_t* e)
{
    if (!ctx) {
        LOG_E("nullptr ctx\n");
        return RET_OK;
    }
    GuiAwtk* p = (GuiAwtk*)ctx;
    // key_event_t *evt = (key_event_t *)e;
    p->KeyDownEvent(nullptr, e);
    return RET_OK;
}
static ret_t IdleQueueGeneral(const idle_info_t* info)
{
    // LOG_I("idle start========================>\n");
    Context* ctx = (Context*)(info->ctx);
    if (ctx->eventCallback) {
        ctx->eventCallback(ctx);
    } else {
        LOG_E("eventCallback is nullptr\n");
    }
    // LOG_I("idle stop========================>\n");
    return RET_OK;
}
static ret_t TimerQueueGeneral(const timer_info_t* timer)
{
    Context* ctx = (Context*)(timer->ctx);
    if (ctx->eventCallback) {
        ctx->eventCallback(ctx);
    } else {
        LOG_E("eventCallback is nullptr\n");
    }
    return RET_REPEAT;
}
static ret_t OnTimer(const timer_info_t* timer)
{
    if (!timer->ctx) {
        LOG_W("ctx is nullptr, timer stop.\n");
        return RET_REMOVE;
    }
    Context* ctx = (Context*)(timer->ctx);
    if (!ctx->timerCallback) {
        LOG_E("timerCallback is nullptr\n");
        return RET_REMOVE;
    }
    if (ctx->timerCallback(ctx)) {
        /**
         * @brief 
         * 回调函数返回true，定时器继续。
         */
        return RET_REPEAT;
    }
    return RET_REMOVE;
}
static ret_t WidgetOnCallback(void* ctx, event_t* e)
{
    if (!ctx) {
        LOG_E("nullptr pointer.\n");
        return RET_OK;
    }
    Context* p = (Context*)(ctx);
    if (p->eventCallback) {
        p->eventCallback(ctx);
    } else {
        LOG_E("eventCallback is nullptr\n");
    }
    return RET_OK;
}
void CreateGuiAwtkInstance()
{
    // GuiEngine::GetInstance().VeritableInstance<GuiAwtk>();
    std::shared_ptr<GuiEngine> ins = std::dynamic_pointer_cast<GuiEngine>(std::make_shared<GuiAwtk>());
    GuiEngine::GetInstance(&ins);
}
GuiAwtk::GuiAwtk()
{
    eventCallback = nullptr;
    for (int i = 0; i < static_cast<int>(WidgitOnEvent::EVT_END); i++) {
        evtMap[i] = EVT_WINDOW_OPEN;
    }
    evtMap[static_cast<int>(WidgitOnEvent::EVT_WINDOW_CLOSE)] = EVT_WINDOW_CLOSE;
    evtMap[static_cast<int>(WidgitOnEvent::EVT_WINDOW_TO_BACKGROUND)] = EVT_WINDOW_TO_BACKGROUND;
    evtMap[static_cast<int>(WidgitOnEvent::EVT_WINDOW_TO_FOREGROUND)] = EVT_WINDOW_TO_FOREGROUND;
    KeymapInit();
}
GuiAwtk::~GuiAwtk()
{
    RegisterKeyDownEvent(nullptr);
}
#if defined(WIN32) && !defined(MINGW)
extern int WINAPI wWinMain99(HINSTANCE hinstance, HINSTANCE hprevinstance, LPWSTR lpcmdline, int ncmdshow, void (*appInit)());
bool GuiAwtk::Init(LPWSTR lpcmdline, void (*appInit)())
{
    auto func = [](LPWSTR lpcmdline, void (*appInit)()) {
        wWinMain99(NULL, NULL, lpcmdline, 0, appInit);
    };
    std::thread t(func, lpcmdline, appInit);
    t.detach(); //设置线程分离属性，线程结束后自动释放资源
    /**
     * @brief Construct a new while object
     * 确定初始化完成后再返回
     */
    while (!initDone) {
        log_info("Waiting for awtk init.\n");
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
    log_info("awtk init succeeded.\n");
    return true;
}
#else
/**
 * @brief
 * linux 系统awtk初始化函数，初始化分辨率。
 * @param width 屏幕分辨率宽度
 * @param height 屏幕分辨率高度
 * @param resPath 资源在系统的绝对路径
 * @return int
 */
extern int AwtkMain(unsigned int width, unsigned int height, const char* resPath, void (*appInit)());
bool GuiAwtk::Init(unsigned int width, unsigned int height, std::string resPath, void (*appInit)())
{
    auto func = [](unsigned int width, unsigned int height, std::string resPath, void (*appInit)()) {
        AwtkMain(width, height, resPath.c_str(), appInit);
    };
    std::thread t(func, width, height, resPath, appInit);
    t.detach(); //设置线程分离属性，线程结束后自动释放资源
    /**
     * @brief Construct a new while object
     * 确定初始化完成后再返回
     */
    while (!initDone) {
        log_info("Waiting for awtk init.\n");
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
    log_info("awtk init succeeded.\n");
    return true;
}
#endif
extern bool AwtkQuit();
void GuiAwtk::UnInit()
{
    log_info("awtk uninit\n");
    AwtkQuit();
    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
Widget* GuiAwtk::NewWidget()
{
    AwtkWidget* p = new AwtkWidget;
    return p;
}
void GuiAwtk::RegisterKeyDownEvent(void (*event)(KeyboardValue value))
{
    widget_t* wm = window_manager();
    if (event) {
        widget_on(wm, EVT_KEY_DOWN, OnKeyDownEvent, this);
        eventCallback = event;
    } else {
        widget_on(wm, EVT_KEY_DOWN, nullptr, nullptr);
        eventCallback = nullptr;
    }
}
Widget* GuiAwtk::WindowOpen(std::string windowName)
{
    AwtkWidget* widget = new AwtkWidget;
    widget->widget = window_open(windowName.c_str());
    if (!widget->widget) {
        LOG_E("awtk window open failed[%s]\n", windowName.c_str());
        delete widget;
        return nullptr;
    }
    LOG_I("open a window(%s).\n", widget->Name().c_str());
    return widget;
}
bool GuiAwtk::WindowClose(Widget* info)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (window_close(awtkInfo.widget) == RET_OK) {
        return true;
    } else {
        LOG_E("page close failed\n");
        return false;
    }
}
void GuiAwtk::KeyDownEvent(void* ctx, event_t* e)
{
    key_event_t* evt = (key_event_t*)e;
    if (eventCallback && keyMap.find(evt->key) != keyMap.end()) {
        eventCallback(keyMap[evt->key]);
    } else {
        LOG_E("key down event is nullptr\n");
    }
}
bool GuiAwtk::IdleQueue(void* ctx)
{
    idle_queue(IdleQueueGeneral, ctx);
    return true;
}
bool GuiAwtk::TimerQueue(void* ctx, unsigned int duration)
{
    timer_queue(TimerQueueGeneral, ctx, duration);
    return true;
}
unsigned int GuiAwtk::TimerAdd(void* ctx, unsigned int duration)
{
    return timer_add(OnTimer, ctx, duration);
}
unsigned int GuiAwtk::TimerCount()
{
    return timer_count();
}
bool GuiAwtk::TimerModiy(unsigned int timerId, unsigned int duration)
{
    if (timer_modify(timerId, duration) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::Lookup(Widget* info, const char* name, bool recursive, Widget* value)
{
    if (!info || !value) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    widget_t* widget = widget_lookup(awtkInfo.widget, name, recursive);
    if (widget) {
        awtkValue.widget = widget;
        return true;
    } else {
        // LOG_W("widget lookup failed.target=[%s]\n", name);
        return false;
    }
}
bool GuiAwtk::SlideMenuSetValue(Widget* widget, unsigned int value)
{
    if (!widget) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkWidget = *(AwtkWidget*)(widget);
    if (slide_menu_set_value(awtkWidget.widget, value) == RET_OK) {
        return true;
    } else {
        LOG_E("slide_menu_set_value failed\n");
        return false;
    }
}
bool GuiAwtk::UseStyle(Widget* info, const char* style)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_use_style(awtkInfo.widget, style) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetTrText(Widget* info, const char* trText)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_tr_text(awtkInfo.widget, trText) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetText(Widget* info, const char* text)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    wchar_t* wText = nullptr;
    /**
     * @brief
     * 转换时必须多一个字节，否则tk_utf8_to_utf16接口会崩溃。
     */
    wText = (wchar_t*)malloc((strlen(text) + 1) * 2);
    if (!wText) {
        LOG_E("malloc failed.\n");
        return false;
    }
    tk_utf8_to_utf16(text, wText, (strlen(text) + 1) * 2);
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_text(awtkInfo.widget, wText) == RET_OK) {
        free(wText);
        return true;
    } else {
        free(wText);
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetTextUtf8(Widget* info, const char* text)
{

    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_text_utf8(awtkInfo.widget, text) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::GetTextUtf8(Widget* info, char* text, unsigned int size)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_get_text_utf8(awtkInfo.widget, text, size) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::CreateAnimator(Widget* info, const char* animation)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_create_animator(awtkInfo.widget, animation) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetVisible(Widget* info, bool visible)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_visible(awtkInfo.widget, visible) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::ViewCreate(Widget* info, int x, int y, int w, int h, Widget* value)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    awtkValue.widget = view_create(awtkInfo.widget, x, y, w, h);
    if (awtkValue.widget) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::LabelCreate(Widget* info, int x, int y, int w, int h, Widget* value)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    awtkValue.widget = label_create(awtkInfo.widget, x, y, w, h);
    if (awtkValue.widget) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetSelfLayout(Widget* info, const char* params)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_self_layout(awtkInfo.widget, params) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetChildrenLayout(Widget* info, const char* params)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_children_layout(awtkInfo.widget, params) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetName(Widget* info, const char* name)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_name(awtkInfo.widget, name) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::Destroy(Widget* info)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_destroy(awtkInfo.widget) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::Move(Widget* info, int x, int y)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_move(awtkInfo.widget, x, y) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SliderSetValue(Widget* info, double value)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (slider_set_value(awtkInfo.widget, value) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::CreateImage(Widget* info, int x, int y, unsigned int w, unsigned int h, Widget* value)
{
    if (!info || !value) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    awtkValue.widget = image_create(awtkInfo.widget, x, y, w, h);
    if (!awtkValue.widget) {
        LOG_E("awtk image create failed\n");
        return false;
    }
    return true;
}
bool GuiAwtk::SetImage(Widget* info, const char* name)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (image_set_image(awtkInfo.widget, name) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::ImageSetDrawType(Widget* info, DrawType drawType)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (image_set_draw_type(awtkInfo.widget, static_cast<image_draw_type_t>(drawType)) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::LocaleInfoTr(const char* trText, char** outText)
{
    // if (!info || !outText)
    // {
    //     LOG_E("nullptr pointer.\n");
    //     return false;
    // }
    // AwtkWidget &awtkInfo = *(AwtkWidget *)(info);
    // *outText = (char *)locale_info_tr(widget_get_locale_info(awtkInfo.widget), trText);
    *outText = (char*)locale_info_tr(locale_info(), trText);
    return true;
}
bool GuiAwtk::LanguageChange(const char* language)
{
    char country[3];
    char lan[3];
    strncpy(lan, language, 2);
    strncpy(country, language + 3, 2);
    locale_info_change(locale_info(), lan, country);
    return true;
}
bool GuiAwtk::EditSetFocus(Widget* info, bool focus)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (edit_set_focus(awtkInfo.widget, focus) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::EditSetCursor(Widget* info, unsigned int cursor)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (edit_set_cursor(awtkInfo.widget, cursor) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::EditSetReadOnly(Widget* info, bool readOnly)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (edit_set_readonly(awtkInfo.widget, readOnly) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::EditSetTrTips(Widget* info, const char* trTips)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (edit_set_tr_tips(awtkInfo.widget, trTips) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetFloating(Widget* info, bool floating)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_floating(awtkInfo.widget, floating) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
unsigned int GuiAwtk::WidgetOn(Widget* info, WidgitOnEvent evt, Context* ctx)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return 0;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    return widget_on(awtkInfo.widget, evtMap[static_cast<int>(evt)], WidgetOnCallback, ctx);
}
bool GuiAwtk::SetLineWrap(Widget* info, bool wrap)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (label_set_line_wrap(awtkInfo.widget, wrap) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetWordWrap(Widget* info, bool wrap)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (label_set_word_wrap(awtkInfo.widget, wrap) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::LabelSetLength(Widget* info, int length)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (label_set_length(awtkInfo.widget, length) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}

bool GuiAwtk::HscrollLabelCreate(Widget* info, int x, int y, int w, int h, Widget* value)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    awtkValue.widget = hscroll_label_create(awtkInfo.widget, x, y, w, h);
    if (awtkValue.widget) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}

bool GuiAwtk::HscrollLabelStop(Widget* value)
{
    if (value == NULL) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    LOG_E("HscrollLabelStop2\n\r");
    if (awtkValue.widget) {
        LOG_E("HscrollLabelStop3\n\r");
        hscroll_label_stop(awtkValue.widget);
    }
    return true;
}

bool GuiAwtk::HscrollLabelSetDuration(Widget* info, int duration)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_duration(awtkInfo.widget, duration) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::HscrollLabelSetElipses(Widget* info, bool ellipses)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_ellipses(awtkInfo.widget, ellipses) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::HscrollLabelSetLoop(Widget* info, bool loop)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_loop(awtkInfo.widget, loop) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::HscrollLabelSetLull(Widget* info, bool lull)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_lull(awtkInfo.widget, lull) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::HscrollLabelSetOnlyFocus(Widget* info, bool onlyFocus)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_only_focus(awtkInfo.widget, onlyFocus) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::HscrollLabelSetOnlyParentFocus(Widget* info, bool onlyFocus)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (hscroll_label_set_only_parent_focus(awtkInfo.widget, onlyFocus) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::SetFocus(Widget* info, bool focus)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_set_focused(awtkInfo.widget, focus) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::DestroyChildren(Widget* info)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (widget_destroy_children(awtkInfo.widget) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::MleditSetCursor(Widget* info, unsigned int cursor)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (mledit_set_cursor(awtkInfo.widget, cursor) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::MleditSetFocus(Widget* info, bool focus)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (mledit_set_focus(awtkInfo.widget, focus) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::MleditSetOpenImWhenFocused(Widget* info, bool open)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (mledit_set_open_im_when_focused(awtkInfo.widget, open) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::DigitClockCreate(Widget* info, int x, int y, int w, int h, Widget* value)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    AwtkWidget& awtkValue = *(AwtkWidget*)(value);
    awtkValue.widget = digit_clock_create(awtkInfo.widget, x, y, w, h);
    if (awtkValue.widget) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
bool GuiAwtk::DigitClockSetFormat(Widget* info, const std::string& format)
{
    if (!info) {
        LOG_E("nullptr pointer.\n");
        return false;
    }
    printf("-------------->%s, format=%s\n", __FUNCTION__, format.c_str());
    AwtkWidget& awtkInfo = *(AwtkWidget*)(info);
    if (digit_clock_set_format(awtkInfo.widget, format.c_str()) == RET_OK) {
        return true;
    } else {
        LOG_E("awtk func failed\n");
        return false;
    }
}
AwtkWidget::AwtkWidget()
{
    widget = nullptr;
}
AwtkWidget::~AwtkWidget()
{
}
bool AwtkWidget::IsValid()
{
    if (widget) {
        return true;
    } else {
        return false;
    }
}
std::string AwtkWidget::Name()
{
    std::string name = "null";
    if (widget) {
        name = widget->name;
    }
    return name;
}
void AwtkWidget::SetWidget(Widget* p)
{
    if (!p) {
        // LOG_E("nullptr pointer.\n");
        return;
    }
    AwtkWidget& awtkInfo = *(AwtkWidget*)(p);
    widget = awtkInfo.widget;
}
int AwtkWidget::GetCoordX()
{
    if (widget) {
        return widget->x;
    }
    return -1;
}
int AwtkWidget::GetCoordY()
{
    if (widget) {
        return widget->y;
    }
    return -1;
}